package com.sinosoft.content;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sinosoft.base.Constants;
import com.sinosoft.content.commen.ContentCommon;
import com.sinosoft.util.CheckEmpty;

@Service("contentManager")
public class ContentManagerImpl implements ContentManager {

	private ContentDao conDao;
	private ContentEnDao conEnDao;

	@Autowired
	public void setConDao(ContentDao conDao) {
		this.conDao = conDao;
	}

	@Autowired
	public void setConEnDao(ContentEnDao conEnDao) {
		this.conEnDao = conEnDao;
	}

	/**
	 * 获取所有中文或英文新闻
	 */
	public List getAllContent(String type) {
		if (!CheckEmpty.isEmpty(type)) {
			if (type.equals(Constants.TABLE_TYPE_CONTENT_EN)) {
				return conEnDao.getAllContent();
			} else {
				return conDao.getAllContent();
			}
		}
		return null;
	}

	/**
	 * 根据id 语言获取新闻
	 */
	public ContentView getContentById(Long conId, String type) {
		ContentView vo = null;
		if (!CheckEmpty.isEmpty(type)) {
			ContentEn en = null;
			Content zh = null;
			if (type.equals(Constants.TABLE_TYPE_CONTENT_EN)) {
				en = conEnDao.getContentById(conId);
			} else {
				zh = conDao.getContentById(conId);
			}
			vo = ContentCommon.toVo(zh, en);
		}
		return vo;
	}

	/**
	 * 根据id 语言类型删除新闻
	 */
	public void removeContentEn(Long id, String type) {
		if (!CheckEmpty.isEmpty(type)) {
			if (type.equals(Constants.TABLE_TYPE_CONTENT_EN)) {
				ContentEn en = conEnDao.getContentById(id);
				conEnDao.removeContentEn(en);
			} else {
				Content zh = conDao.getContentById(id);
				conDao.removeContent(zh);
			}
		}
	}

	/**
	 * 添加新闻
	 */
	public ContentView saveContent(ContentView con, String type) {
		ContentView vo = null;
		if (!CheckEmpty.isEmpty(type)) {
			ContentEn en = null;
			Content zh = null;
			if (type.equals(Constants.TABLE_TYPE_CONTENT_EN)) {
				en = ContentCommon.toEn(null, con);
				en = conEnDao.saveContent(en);
			} else {
				zh = ContentCommon.toZh(null, con);
				zh = conDao.saveContent(zh);
			}
			vo = ContentCommon.toVo(zh, en);
		}
		return vo;
	}

	/**
	 * 根据文章类型获取最新的一条数据在首页展示
	 */
	public List getContentByType(ContentView view, String lantype) {
		List list = null;
		try {
			if (lantype.equals(Constants.TABLE_TYPE_CONTENT_EN)) {
				list = conEnDao.getContentByType(view);
			} else {
				list = conDao.getContentByType(view);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	public int getContentNum(ContentView vo) {
		int resultnum = 0;
		try {
			StringBuffer sqlsb = new StringBuffer();
			if (vo.getLanguage().equals(Constants.TABLE_TYPE_CONTENT_EN)) {
				sqlsb.append("select count(*) from ContentEn where 1=1 ");
			} else {
				sqlsb.append("select count(*) from Content where 1=1 ");
			}

			if (null != vo) {
				if (!CheckEmpty.isEmpty(vo.getContentTitle())) {
					sqlsb.append(" and contentTitle like '%"
							+ vo.getContentTitle() + "%'");
				}
				if (null != vo.getContentType() && vo.getContentType() > 0) {
					sqlsb.append(" and contentType = " + vo.getContentType());
				}
				if (null != vo.getIsRelease() && vo.getIsRelease() > 0) {
					sqlsb.append(" and isRelease = " + vo.getIsRelease());
				}
				if (!CheckEmpty.isEmpty(vo.getStartime())) {
					sqlsb.append(" and contentTime >= to_date('"
							+ vo.getStartime() + "','yyyy-mm-dd hh24:mi:ss')");
				}
				if (!CheckEmpty.isEmpty(vo.getEndtime())) {
					sqlsb.append(" and contentTime <= to_date('"
							+ vo.getEndtime() + "','yyyy-mm-dd hh24:mi:ss')");
				}
			}
			String sql = sqlsb.toString();
			resultnum = conDao.getCountNum(sql);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultnum;

	}
}
